package com.puhui.education.activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.puhui.education.R;
import com.puhui.education.activity.base.BaseFragmentAcy;
import com.puhui.education.adapter.MyPageAdapter;
import com.puhui.education.fragment.course.CourseBriefFrag;
import com.puhui.education.fragment.course.CourseCatalogFrag;
import com.puhui.education.fragment.course.CourseCommentFrag;
import com.puhui.education.util.ScreenUtil;
import com.zhy.android.percent.support.PercentLinearLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/6/14.
 */
public class MyCourseAcy extends BaseFragmentAcy {
    @Bind(R.id.tl_title_myCourseAcy)
    TabLayout tl_title;
    @Bind(R.id.vp_showFragment_myCourseAcy)
    ViewPager vp_showFragment;

    private List<String> titleList;
    private MyPageAdapter pageAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.acy_my_course);
        ButterKnife.bind(this, this);
        initData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }


    @Override
    protected void initData() {
        super.initData();
        titleList = new ArrayList<>();
        titleList.add("全部");
        titleList.add("学习中");
        titleList.add("未学习");
        titleList.add("已学完");
        titleList.add("已过期");
        initViewPager();
        tl_title.setupWithViewPager(vp_showFragment);//将TabLayout和ViewPager关联起来
        initTab();
    }

    private void initTab() {
        for (int i = 0; i < titleList.size(); i++) {
            tl_title.getTabAt(i).setCustomView(getTabView(i));
        }
    }


    public View getTabView(int position) {
        View view = LayoutInflater.from(this).inflate(R.layout.cv_tab_layout, null);
        PercentLinearLayout ppl = (PercentLinearLayout) view.findViewById(R.id.pll_container_tabLayout);
        ppl.setLayoutParams(new PercentLinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int)(ScreenUtil.getScreenHeight()*0.055f)));
        TextView tv = (TextView) view.findViewById(R.id.tv_title_tabLayout);
        tv.setText(titleList.get(position));
        return view;
    }
    private void initViewPager() {
        CourseBriefFrag courseBriefFrag = new CourseBriefFrag();
        CourseCatalogFrag courseCatalogFrag = new CourseCatalogFrag();
        CourseCommentFrag courseCommentFrag = new CourseCommentFrag();
        CourseBriefFrag courseBriefFrag1 = new CourseBriefFrag();
        CourseCatalogFrag courseCatalogFrag2 = new CourseCatalogFrag();
        List<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(courseBriefFrag);
        fragmentList.add(courseCatalogFrag);
        fragmentList.add(courseCommentFrag);
        fragmentList.add(courseBriefFrag1);
        fragmentList.add(courseCatalogFrag2);
        pageAdapter = new MyPageAdapter(getSupportFragmentManager(),fragmentList);
        vp_showFragment.setAdapter(pageAdapter);
        vp_showFragment.setCurrentItem(getIntent().getIntExtra("Page",0));
    }
}
